package egovframework.npqs.pqi.mobile.jsonparser;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.util.Log;
import egovframework.npqs.pqi.mobile.AppStatus;
import egovframework.npqs.pqi.mobile.common.Const;
import egovframework.npqs.pqi.mobile.jsoncontent.ErrorItem;
import egovframework.npqs.pqi.mobile.jsoncontent.QuarantineResultItem;
import egovframework.npqs.pqi.mobile.util.BrUtilManager;
import egovframework.npqs.pqi.mobile.util.ParameterUtil;

public class QuarantineStatusThread extends BasicThread {

	public interface QuarantineStatusThreadCallback {
		public void OnQuarantineStatusCompleted(boolean isNetworkConntected, boolean result, String message);
		public void OnQuarantineStatusError(ErrorItem errorItem);
	}

	private String url;
	private QuarantineStatusThreadCallback callback;
	private HashMap<String, String> searchMap;
	private ArrayList<NameValuePair> post;
	
	/* 파라메터*/
	private boolean isFirst, pageState;
	private String offCd;

	public QuarantineStatusThread(Context context, QuarantineStatusThreadCallback callback, String offCd, String vpnCnId) {
		super(vpnCnId);
		this.context = context;
		this.callback = callback;
		this.offCd = offCd;
		this.appStatus = (AppStatus) context.getApplicationContext();
		this.isFirst = true;
	}
	
	public QuarantineStatusThread(Context context,
			QuarantineStatusThreadCallback callback, HashMap<String, String> searchMap, boolean pageState, String vpnCnId) {
		super(vpnCnId);
		this.context = context;
		this.callback = callback;
		this.appStatus = (AppStatus) context.getApplicationContext();
		this.searchMap = searchMap;
		this.isFirst = false;
		this.pageState = pageState;
	}

	@Override
	public void run() {
		Map<String, String> map = new HashMap<String, String>();
		String sDate = BrUtilManager.getCurrentDateTime(0);
		String eDate = BrUtilManager.getCurrentDateTime(0);
		String param = "";
		if(isFirst){ // 최초 요청
			map.put(Const.SEARCHOFFCD, offCd);
			map.put(Const.SEARCHSDATE, sDate);
			map.put(Const.SEARCHEDATE, eDate);
			param = parameterReturn(Const.REQ013, (HashMap<String, String>) map);
		}else{ // 조건 요청
			map=searchMap;
			param = parameterReturn(Const.REQ013, (HashMap<String, String>) map);
		}
		
		serviceBroker(param, context);
	}

	@Override
	public void settingObject() {
		// 통신불통상태이면
		if(!appStatus.utilm.isNetworkConnected(context)) {
			callback.OnQuarantineStatusCompleted(false, false, "통신 에러");
			return;
		}
		
		try {
			json = new JSONObject(returnJson);
			if(json == null) {	// json 값이 null 일때
				callback.OnQuarantineStatusError(null);
				return;
			}
			int result = json.getInt(Const.RESULT);
			
			ArrayList<QuarantineResultItem> arrQuarantineItem = null;
			if(pageState){
				arrQuarantineItem = appStatus.getArrQuarantineStatusItem();
			}else{
				arrQuarantineItem = new ArrayList<QuarantineResultItem>();
			}
			
			if(result == 0){	// result 값이 0일 경우
				String data = json.getString(Const.DATA);
				
				ErrorItem errorItem = new ErrorItem();
				errorItem.setrDesc(data);

				appStatus.setArrQuarantineStatusItem(arrQuarantineItem);
				callback.OnQuarantineStatusError(errorItem);
			}else{	// result 값이 0이 아닐 경우
				JSONArray arrayLogin = json.getJSONArray(Const.DATA);
				Log.i("arrayLogin", "arrayLogin = " + arrayLogin);
				QuarantineResultItem quarantineItem = null;
				for (int i = 0; i < arrayLogin.length(); i++) {
					JSONObject quarantine = arrayLogin.getJSONObject(i);
					quarantineItem = new QuarantineResultItem();
					quarantineItem.setFieDe(appStatus.utilm.getStringJSONObject(quarantine, QuarantineResultItem.FIEDE));
					quarantineItem.setOffCd(appStatus.utilm.getStringJSONObject(quarantine, QuarantineResultItem.OFFCD));
					quarantineItem.setRecNo(appStatus.utilm.getStringJSONObject(quarantine, QuarantineResultItem.RECNO));
					//quarantineItem.setValidflagNm(appStatus.utilm.getStringJSONObject(quarantine, QuarantineResultItem.VALIDFLAGNM));
					//quarantineItem.setMetNm(appStatus.utilm.getStringJSONObject(quarantine, QuarantineResultItem.METNM));
					quarantineItem.setVenNm(appStatus.utilm.getStringJSONObject(quarantine, QuarantineResultItem.VENNM));
					quarantineItem.setPrdlstNm(appStatus.utilm.getStringJSONObject(quarantine, QuarantineResultItem.PRDLSTNM));
					quarantineItem.setDelayAt(appStatus.utilm.getStringJSONObject(quarantine, QuarantineResultItem.DELAYAT));
					quarantineItem.setDelayNm(appStatus.utilm.getStringJSONObject(quarantine, QuarantineResultItem.DELAYNM));
					quarantineItem.setTotalRows(appStatus.utilm.getStringJSONObject(quarantine, QuarantineResultItem.TOTALROWS));
					
					quarantineItem.setResNo(appStatus.utilm.getStringJSONObject(quarantine, QuarantineResultItem.RESNO));
					quarantineItem.setResOffCd(appStatus.utilm.getStringJSONObject(quarantine, QuarantineResultItem.RESOFFCD));
					quarantineItem.setProgrsNm(appStatus.utilm.getStringJSONObject(quarantine, QuarantineResultItem.PROGRSNM));
					
					arrQuarantineItem.add(quarantineItem);
				}
				
				appStatus.setArrQuarantineStatusItem(arrQuarantineItem);
				callback.OnQuarantineStatusCompleted(true, true, mResult);
			}
		} catch (JSONException e1) {
			e1.printStackTrace();
		}
	}
}
